
clear all
set more off
 
*cd /data2/cedsei/firm/

/* Inputs */ 

local in_wagesurvey "/home/projects/cedsei/data/A1_clean_wagesurvey_00_19"
local in_yob1 	    "/data2/cedsei/firm/yob.dta"
local in_yob2 	    "/data2/cedsei/data2/yearofbirth"

// From A2_rams
local in_rams 	    "/data2/cedsei/firm/rams_ourpop.dta"
// From A1_definepop
local in_indpop     "/data2/cedsei/firm/not_ind_pop.dta"

/* Outputs */ 

local out_firm 	    "/data2/cedsei/firm/firmlist"
local out_wc 	    "/data2/cedsei/firm/wc"
local out_yob 	    "/data2/cedsei/firm/yob"
local out_ramspanel "/data2/cedsei/firm/rams_ourpop_cleaned"
local out_djnot     "/data2/cedsei/firm/dj_not"
local out_dj	    "/data2/cedsei/firm/notificatio_dj"
local out_df	    "/data2/cedsei/firm/notificatio_df"


********************************************************************************
local prep = 0
if `prep'==1 {
clear
use persid firmid year sekt using  "`in_wagesurvey'"
rename persid lopnr 
rename firmid lopnr_peorgnr 
gen wc = sekt=="Tj"
gcollapse (max) wc, by(lopnr lopnr_peorgnr)
save `out_wc', replace

clear 
use			 "`in_yob1'"
merge 1:1 lopnr using    "`in_yob2'", nogen 
replace yob = ym(fodelsear,1) if mi(yob)
save `out_yob', replace 

}

clear 
use "`in_indpop'"
merge m:1 lopnr using `out_yob', keep(3) nogen
merge m:1 lopnr lopnr_peorgnr using `out_wc', keep(1 3)  nogen
replace wc = 0 if mi(wc)
merge 1:1 lopnr_peorgnr lopnr year using "`in_rams'", keepusing(manfran mantill cumten tenure)
drop if _merge==2

gen keeping = cond(inrange(month(list_dat),manfran,mantill),1,0)
replace keeping = 0 if _merge==1
egen keepmean = mean(keeping), by(lopnr_peorgnr)
count if lopnr_peorgnr==2434
count
keep if keepmean>.95
count
// We drop entire firms who have a high share of notified individuals not 
// working for the notifying firm when the list arrives. 
count if lopnr_peorgnr==2434
drop if _merge==1 
drop _merge keeping keepmean 

gen cumtenatnot = cumten - tenure + month(list_dat)

gen ageatnot = (list_dat_ym - yob)/12
gen de_jure = ///
cond(inrange(cumtenatnot,0,23),1, ///
cond(inrange(cumtenatnot,24,47),2, ///
cond(inrange(cumtenatnot,48,71),3, ///
cond(inrange(cumtenatnot,72,95),4, ///
cond(inrange(cumtenatnot,96,119),5, ///
cond(cumtenatnot>=120,6,0))))))
replace de_jure = 12 if ageatnot>=55 & !mi(ageatnot) & !mi(cumtenatnot) & wc==1
replace de_jure = 1 if ageatnot>=67 & !mi(ageatnot) & !mi(cumtenatnot)

gen de_facto = anstupp_dat_ym - list_dat_ym 

bys lopnr_peorgnr year: gen n_not = _N
keep lopnr lopnr_peorgnr year list_dat* anstupp_dat* de_jure de_facto n_not 

isid lopnr lopnr_peorgnr year 
save "`out_djnot'", replace

keep lopnr_peorgnr 
duplicates drop 
save `out_firm', replace 

/*******************************************************************************

The below reshapes the dj_not (`out_djnot') data to split times into calendar 
years. 

We also take the rams data and remove the firms who get removed when cleaning 
out those with a high share notified individuals not working for the notifying
firm at the time of the list arrival.

*******************************************************************************/

clear 
use lopnr lopnr_peorgnr list_dat_ym anstupp_dat_ym using "`out_djnot'"
gen y1 = year(dofm(list_dat_ym))
gen y2 = year(dofm(anstupp_dat_ym))
gen diff = y2 - y1 + 1
expand diff
bys lopnr_peorgnr lopnr: gen year = y1 + _n - 1
gen manfran_df = /// 
cond(y1==year, month(dofm(list_dat_ym)), 1)
gen mantill_df = ///
cond(y2==year, month(dofm(anstupp_dat_ym)), 12)
keep lopnr lopnr_peorgnr year manfran_df mantill_df 
gcollapse (min) manfran_df (max) mantill_df, by(lopnr lopnr_peorgnr year)
save "`out_df'", replace

clear 
use lopnr lopnr_peorgnr list_dat_ym de_jure using "`out_djnot'"
gen anstupp_dat_ym = list_dat_ym + de_jure
format anstupp_dat_ym %tm
gen y1 = year(dofm(list_dat_ym))
gen y2 = year(dofm(anstupp_dat_ym))
gen diff = y2 - y1 + 1
expand diff
bys lopnr_peorgnr lopnr: gen year = y1 + _n - 1
gen manfran_dj = /// 
cond(y1==year, month(dofm(list_dat_ym)), 1)
gen mantill_dj = ///
cond(y2==year, month(dofm(anstupp_dat_ym)), 12)
keep lopnr lopnr_peorgnr year manfran_dj mantill_dj
gcollapse (min) manfran_dj (max) mantill_dj, by(lopnr lopnr_peorgnr year)
save "`out_dj'", replace

clear 
use "`in_rams'"
merge m:1 lopnr_peorgnr using `out_firm' 
keep if _merge==3
drop _merge 
save "`out_ramspanel'", replace 

 



